package com.example.demo.controller;

import com.example.demo.domain.Hotel;
import com.example.demo.domain.RoomType;
import com.example.demo.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/hotel")
public class HotelController {
    @Autowired
    private HotelService hotelService;
    private String dest;
    private String key;

    @GetMapping("/search")
    public  String userPage(){
        return "/pages/hotel/search";
    }


    @PostMapping("/search")
    public String xixi(HttpServletRequest request,@RequestParam("destination") String dest,@RequestParam("key")String key , Model model){//timeCheckIn timeCheckOut roomnum star
        ArrayList<Hotel> a= (ArrayList<Hotel>) hotelService.findHotel(dest,  key);
        model.addAttribute("hotels",a);
       HttpSession session=request.getSession();
       session.setAttribute("dest",dest);
       session.setAttribute("key",key);
        return "/pages/hotel/searchresult";
    }

    @GetMapping("/detail/{id}")
    public  String hotelshow(@PathVariable("id") String id, Model model){
        Long ID=Long.parseLong(id);
        Hotel hotel=hotelService.findhotelbyid(ID);
        ArrayList<RoomType>roomtypes=hotelService.findtypebyid(id);
        model.addAttribute("hotel",hotel);
        model.addAttribute("roomtypes",roomtypes);

        return "/pages/hotel/detail.html";
    }

    @PostMapping("/searchdetail")
    public String serachdetail(HttpServletRequest request,@RequestParam("pricelow") String lowprice
            ,@RequestParam("pricehigh") String highprice,@RequestParam("breakfast") int breakfast
            ,@RequestParam("score")  String score,@RequestParam("star") int star, Model model){
        HttpSession session= request.getSession();
        String dest= session.getAttribute("dest").toString();
        String key= session.getAttribute("key").toString();
        double pricelow,pricehigh,scored;

        if(lowprice=="")pricelow=0;
        else pricelow=Double.parseDouble(lowprice);

        if(highprice=="")pricehigh=10000;
        else pricehigh=Double.parseDouble(highprice);

        if(score=="")scored=0;
        else scored=Double.parseDouble(score);


        ArrayList<Hotel> hotels=(ArrayList<Hotel>) hotelService.findHoteldetail(dest,key,star,breakfast,scored,pricelow,pricehigh);
//        hotels.remove(2);
      model.addAttribute("hotels",hotels);

        //System.out.println(a);
        return "/pages/hotel/searchresult";
    }

    @ResponseBody
    public List<Hotel> allHotel(){
        return hotelService.allHotel();
    }

    @RequestMapping
    public String hotelPage(Model model){
        List<Hotel> hotels=hotelService.allHotel();
        model.addAttribute("hotels",hotels);
        return "pages/hotel/list.html";
    }

}